/*
 * @Description: 利用权限列表表格组件
 * @Author: Rfan
 * @Date: 2022-04-07 15:49:26
 * @LastEditTime: 2022-05-25 09:01:13
 */

import { FC, useEffect } from 'react';
import { Checkbox, Form, Typography } from 'antd';
import classNames from 'classnames/bind';
import { useUseRightContext } from '../../contexts/UseRightContext';

import styles from '../../styles/list.module.scss';

const cx = classNames.bind(styles);

const { Text } = Typography;

export type RuleTableProps = {
  title?: string;
  data?: any;
};

const ListContent: FC<RuleTableProps> = ({ data }) => {
  const { selectAT, rightsList, getRightsList, saveFormLink, saveRights } = useUseRightContext();
  const [form] = Form.useForm();

  useEffect(() => {
    saveFormLink(form);
  }, []);

  useEffect(() => {
    // 请求数据
    if (selectAT.id) {
      getRightsList(selectAT.id);
    }
  }, [selectAT]);

  useEffect(() => {
    // 请求数据
    if (rightsList.length > 0) {
      const formData = rightsList[0] || {};
      for (const key in formData) {
        if (Object.prototype.hasOwnProperty.call(formData, key)) {
          const ele = formData[key];
          if (['id', 'archiveTypeId', 'extendRights'].indexOf(key) <= -1) {
            formData[key] = ele === 0;
          }
        }
      }
      form.setFieldsValue(rightsList[0]);
    } else {
      form.resetFields();
    }
  }, [rightsList]);

  const onFinish = (values: any) => {
    const data = rightsList[0] || {};
    !data.id && (data.archiveTypeId = selectAT.id);
    for (const key in values) {
      if (Object.prototype.hasOwnProperty.call(values, key)) {
        const ele = values[key];
        values[key] = ele === 0 || ele === true ? 0 : 1;
      }
    }
    console.log('add', values, { ...data, ...values });
    saveRights({ ...data, ...values });
  };

  return (
    <div className={cx('list-content')}>
      <p>默认利用操作权限</p>
      <Form form={form} onFinish={onFinish}>
        <p>
          <Form.Item name="allowPreview" valuePropName="checked" noStyle>
            <Checkbox>电子原文浏览</Checkbox>
          </Form.Item>
          <Text type="secondary">没有权限的用户只能看到附件名称，但是不能在线浏览</Text>
        </p>
        <p>
          <Form.Item name="allowPrint" valuePropName="checked" noStyle>
            <Checkbox>电子原文打印</Checkbox>
          </Form.Item>
          <Text type="secondary">无权限则下载文件不能被打印（通常是PDF文件）</Text>
        </p>
        <p>
          <Form.Item name="allowDownload" valuePropName="checked" noStyle>
            <Checkbox>电子原文下载</Checkbox>
          </Form.Item>
          <Text type="secondary">没有权限则不能对附件进行下载</Text>
        </p>
        <p>
          <Form.Item name="allowBorrow" valuePropName="checked" noStyle>
            <Checkbox>档案实物借阅</Checkbox>
          </Form.Item>
          <Text type="secondary">有权限管理员就可解除，无权限则档案的借阅需要领导审批</Text>
        </p>
      </Form>
    </div>
  );
};

export default ListContent;
